﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class CompanyService : EntityService<Company>
    {
        public IEnumerable<Company> GetAllCompanyNames()
        {
            var companiesCursor = this.MongoConnectionHandler.MongoCollection.FindAllAs<Company>()
                .SetSortOrder(SortBy<Company>.Ascending(g => g.CompanyID))
                .SetFields(Fields<User>.Include(g => g.Id, g => g.UserName, g => g.UserType, g => g.Password, g => g.CreateDate));
            return companiesCursor;
        }

        public List<Company> GetAllCompanies()
        {
            return (from company in this.MongoConnectionHandler.MongoCollection.FindAllAs<Company>()
                    select company).ToList();
        }

        private List<SelectListItem> GetCompanyList()
        {
            List<SelectListItem> CompaniesList = new List<SelectListItem>();

            List<Company> AllCompaniesList = GetAllCompanies().OrderBy(x => x.CompanyID).ToList();

            foreach (Company company in AllCompaniesList)
            {
                SelectListItem sListItem = new SelectListItem();
                sListItem.Text = company.CompanyID + ". " + company.CompanyName;
                sListItem.Value = company.CompanyID.ToString();

                CompaniesList.Add(sListItem);
            }

            return CompaniesList;
        }

        public Company GetCompanyByCompanyID(int CompanyID)
        {
            return (from company in this.MongoConnectionHandler.MongoCollection.FindAllAs<Company>()
                    where company.CompanyID == CompanyID
                    select company).FirstOrDefault();
        }

        public IEnumerable<SelectListItem> GetCompanyNameList()
        {
            return GetCompanyList();
        }

        public override void Update(Company entity)
        {
            //// Not necessary for the example
        }
    }
}